package com.lib.basex.widget.border;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.InsetDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

import java.util.Objects;

public class LViewStyle {

    public static final int LINE_LEFT = 8;
    public static final int LINE_TOP = 4;
    public static final int LINE_RIGHT = 2;
    public static final int LINE_BOTTOM = 1;
    public static final int LINE_ALL = 15;

    public int width = 1;
    @ColorInt
    public int color = Color.TRANSPARENT;
    // 0
    public int direct;

    public int radius;

    @ColorInt
    public int strokeColor = Color.TRANSPARENT;

    public int leftTopRadius, rightTopRadius, leftBottomRadius, rightBottomRadius;

    public LViewStyle() {

    }

    public LViewStyle(int color, int borderColor, int width, int direct, int radius, int leftBottomRadius, int leftTopRadius, int rightBottomRadius, int rightTopRadius) {
        this.color = color;
        this.strokeColor = borderColor;
        this.width = width;
        this.direct = direct;
        this.radius = radius;
        this.leftBottomRadius = leftBottomRadius;
        this.leftTopRadius = leftTopRadius;
        this.rightBottomRadius = rightBottomRadius;
        this.rightTopRadius = rightTopRadius;
    }

    public Drawable toDrawable() {
        GradientDrawable drawable = new GradientDrawable();
        setRadius(drawable);
        drawable.setColor(color);

        if (direct == LINE_ALL && width > 0) {
            drawable.setStroke(width, strokeColor);
            return drawable;
        }

        if (direct == 0 || width == 0 || strokeColor == 0) {
            return drawable;
        }
        GradientDrawable borderDrawable = new GradientDrawable();
        borderDrawable.setColor(strokeColor);
        setRadius(borderDrawable);

        int left = (direct & LINE_LEFT) == LINE_LEFT ? width : 0;
        int top = (direct & LINE_TOP) == LINE_TOP ? width : 0;
        int right = (direct & LINE_RIGHT) == LINE_RIGHT ? width : 0;
        int bottom = (direct & LINE_BOTTOM) == LINE_BOTTOM ? width : 0;
        InsetDrawable insetDrawable = new InsetDrawable(drawable, left, top, right, bottom);

        return new LayerDrawable(new Drawable[]{borderDrawable, insetDrawable});
    }

    private void setRadius(GradientDrawable drawable) {
        if (radius != 0) {
            drawable.setCornerRadius(radius);
        } else if (leftTopRadius != 0 || rightTopRadius != 0 || leftBottomRadius != 0 || rightBottomRadius != 0) {
            drawable.setCornerRadii(new float[]{leftTopRadius, leftTopRadius, rightTopRadius, rightTopRadius, rightBottomRadius, rightBottomRadius, leftBottomRadius, leftBottomRadius});
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        LViewStyle that = (LViewStyle) o;
        return width == that.width && color == that.color && direct == that.direct && radius == that.radius && strokeColor == that.strokeColor && leftTopRadius == that.leftTopRadius && rightTopRadius == that.rightTopRadius && leftBottomRadius == that.leftBottomRadius && rightBottomRadius == that.rightBottomRadius;
    }

    public LViewStyle copy(){
        LViewStyle style = new LViewStyle();
        style.rightTopRadius = rightTopRadius;
        style.rightBottomRadius = rightBottomRadius;
        style.leftTopRadius = leftTopRadius;
        style.leftBottomRadius = leftBottomRadius;
        style.direct = direct;
        style.radius = radius;
        style.width = width;
        style.color = color;
        style.strokeColor = strokeColor;
        return style;
    }
}
